<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\OrderProducts;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;

/**
 * 订单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class OrderProductsController extends BaseController
{
    /**
     * @var OrderProductsRepository
     */
    protected $order_products_repo;

    /**
     * @var OrdersRepository
     */
    protected $orders_repo;

    protected $request;

    protected $user_id;

    public function __construct(Request $request, OrderProductsRepository $order_products_repo, OrdersRepository $orders_repo)
    {
        parent::__construct();
        $this->order_products_repo = $order_products_repo;
        $this->user_id = array_get($this->user, 'id');

        $order_id = route_parameter('order_id', 0);
        if(!$order_id){
            return $this->error(ErrorConst::ERROR_CODE_MSG, ErrorConst::ERROR_CODE);
        }

        $order_code = Orders::getOrderColumn($order_id, 'order_code');

        $this->order_products_repo->scopeQuery(function ($query) use($order_code){
            return $query->where(['order_code' => $order_code, 'user_id' => $this->user_id]);
        });

    }

    /**
     * 订单商品列表
     * @return \Illuminate\Support\Collection
     */
    public function index()
    {
        $order_products = $this->order_products_repo->all();
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $order_products['data'], true);
    }

    /**
     * 商品评论
     * @param Request $request
     * @param OrderProductsRepository $order_product_repo
     * @param OrderProductCommentRepository $order_product_comment_repo
     * @param $order_id
     * @param $product_id
     * @return \Illuminate\Support\Collection
     */
    public function updateOrderProducts(Request $request, OrderProductsRepository $order_product_repo, OrderProductCommentRepository $order_product_comment_repo,ProductsRepository $products_repo, $order_id, $order_product_id)
    {
        $this->validate($request, [
            'reputation' => 'required|integer|max:5',
            'service_attitude' => 'required|integer|max:5',
            'delivery_speed' => 'required|integer|max:5',
            'comment' => 'required|string',
        ]);
        $data = $request->all(['reputation', 'service_attitude', 'delivery_speed', 'comment']);

        $order = $this->orders_repo->with(['orderProducts' => function ($sql) use ($order_product_id) {
            $sql->where('id', $order_product_id);
        }])->find($order_id);

        $ret = false;
        $order_product = array_get($order['data'], 'order_products.0');
        if ($order_product && $order_product['is_appraise'] == OrderProducts::IS_APPRAISE_NO) {
            \DB::beginTransaction();
            try {
                //订单产品标记已评论
                $order_product_repo->update(['is_appraise' => OrderProducts::IS_APPRAISE_YES], $order_product_id);
                //添加评论
                $data['user_id'] = $this->user_id;
                $data['order_code'] = $order['data']['order_code'];
                $data['product_id'] = $order_product['product_id'];
                $data['order_product_id'] = $order_product_id;
                $data['sku_id'] = $order_product['sku_id'];
                $order_product_comment_repo->create($data);
                //增加商品评论数
                $products_repo->incrementCommentCount($order_product['product_id']);

                $ret = true;
                \DB::commit();
            } catch (\Exception $e) {
                \DB::rollback(); //事务回滚;
            }
        }
        return $this->noDataReturn($ret);
    }
}
